package org.ws.edu.task1.oksana;

import org.ws.edu.task1.ICalculator;
import org.ws.edu.task1.IConsoleHandler;

public class ConsoleHandlerOk implements IConsoleHandler {
    /*
     * private ICalculator calculator = new CalculatorOk();
     * private static final String[] operator = { "+","-","/","*"};
     * @Override
     * public String handle(String line) {
     * return "= " + spliter(line);
     * }
     * //Рекурсивный метод
     * @SuppressWarnings("unused")
     * public String spliter (String str) {
     * String result = "";
     * String[] s_mass;
     * int j; int i;
     * if (checkString(str)){
     * return "" + (new Double(result) + new Double(str));
     * }
     * else
     * for (i = 0; i < operator.length; i++) {
     * if (str.indexOf(operator[i].charAt(0))!=-1){
     * s_mass = str.split(""+operator[i]);
     * for (j = 0; j < s_mass.length; j++){
     * result = spliter (s_mass[j]);
     * }
     * }
     * return result;
     * }
     * return "= " + result;
     * }
     * // Выполняет операцию с id z над x и y
     * public String operation(String x, String y, int z){
     * double prom_result = 0;
     * switch (z) {
     * case 0:
     * prom_result = plus (new Double(x),new Double(y));
     * break;
     * case 1:
     * prom_result = minus (new Double(x),new Double(y));
     * break;
     * case 2:
     * prom_result = delit (new Double(x),new Double(y));
     * break;
     * case 3:
     * prom_result = umnozh (new Double(x),new Double(y));
     * break;
     * default:
     * break;
     * }
     * return "" + prom_result;
     * }
     * // Сложение
     * public double plus (double a, double b){
     * return a + b;
     * }
     * // Вычетание
     * public double minus (double a, double b){
     * return a - b;
     * }
     * // Деление
     * public double delit (double a, double b){
     * return a / b;
     * }
     * // Умножение
     * public double umnozh (double a, double b){
     * return a * b;
     * }
     * // Является ли строка числом?
     * public boolean ch eckString (String check_str){
     * try {
     * Double.parseDouble(check_str);
     * } catch (Exception e) {
     * return false;
     * }
     * return true;
     * }
     */

    // старый хромоногий калькулятор,
    // который выполняет только один вид операций
    private ICalculator calculator = new CalculatorOk();
    private static final String[] operator = { "+", "-", "*", "/" };

    @Override
    public String handle(String line) {
        // long fact = calculator.factorial(5);

        double sum = 0;
        for (int i = 0; i < operator.length - 1; i++) {
            if (line.indexOf(operator[i].charAt(0)) != -1) {
                String[] mass = line.split("\\" + operator[i]);
                sum = new Double(mass[0]);
                if ("+".equals(operator[i])) {
                    for (int j = 1; j < mass.length; j++) {
                        sum = sum + new Double(mass[j]);
                    }
                    break;
                }
                if ("-".equals(operator[i])) {
                    for (int j = 1; j < mass.length; j++) {
                        sum = sum - new Double(mass[j]);
                    }
                    break;
                }
                if ("/".equals(operator[i])) {
                    for (int j = 1; j < mass.length; j++) {
                        sum = sum / new Double(mass[j]);
                    }
                    break;
                }
                if ("*".equals(operator[i])) {
                    for (int j = 1; j < mass.length; j++) {
                        sum = sum * new Double(mass[j]);
                    }
                    break;
                }
            }
        }
        return "" + sum;
    }
}
